﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using DataTurnClient.Enum;
using DataTurnClient.Server;
using static DataTurnClient.Server.Post<dynamic>;
using SqlSugar;
using ReactiveUI;

namespace DataTurnClient
{
    [SugarTable("JobCentre")]
    public class Job :   INotifyPropertyChanged 
    {
        public enum JobRunType
        {
            /// <summary>
            /// 等待运行
            /// </summary>
            Wating = 1,
            /// <summary>
            /// 运行中
            /// </summary>
            Run = 2,
            /// <summary>
            /// 结束
            /// </summary>
            Over = 3,
        }
        /// <summary>
        /// 任务代码（接口地址）
        /// </summary>
        [SugarColumn(ColumnDataType = "varchar(20)", IsNullable = false)]
        public string JobCode {
            get => _JobCode;
            set
            {
                _JobCode = value;
                NotifyPropertyChanged();
            }
        }
        private string _JobCode;
        /// <summary>
        /// 任务简称
        /// </summary>
        [SugarColumn(ColumnDataType = "varchar(100)", IsNullable = true)]
        public string JobName {
            get => _JobName;
            set
            {
                _JobName = value;
                NotifyPropertyChanged();
            }
        }
        private string _JobName;
        /// <summary>
        /// 任务状态
        /// </summary>
        public int JobStatus
        {
            get => _JobStatus;
            set
            {
                _JobStatus = value;
                NotifyPropertyChanged();
            }
        }
        private int _JobStatus = 0;
        /// <summary>
        /// 任务执行时间字符串
        /// </summary>
        [SugarColumn(ColumnDataType = "varchar(100)", IsNullable = true)]
        public string JobRunTime
        {
            get => _JobRunTime;
            set
            {
                _JobRunTime = value;
                NotifyPropertyChanged();
            }
        }
        private string _JobRunTime;
        /// <summary>
        /// 任务执行星期字符串
        /// </summary>
        [SugarColumn(ColumnDataType = "varchar(25)", IsNullable = true)]
        public string JobWeek
        {
            get => _JobWeek;
            set
            {
                _JobWeek = value;
                NotifyPropertyChanged();
            }
        }
        private string _JobWeek;

        /// <summary>
        /// 任务描述详情
        /// </summary>
        [SugarColumn(ColumnDataType = "varchar(255)", IsNullable = true)]
        public string JobDetail {
            get => _JobDetail;
            set
            {
                _JobDetail = value;
                NotifyPropertyChanged();
            }
        }
        private string _JobDetail;
        /// <summary>
        /// 任务运行的当前状态
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public JobRunType JobRunStatus
        {
            get => _JobRunStatus;
            set
            {
                _JobRunStatus = value;
                NotifyPropertyChanged();
            }
        }
        private JobRunType _JobRunStatus = JobRunType.Wating;
        
        /// <summary>
        /// 任务路由
        /// </summary>
        [SugarColumn(ColumnDataType = "varchar(5)", IsNullable = true)]
        public string JobRoute
        {
            get => _JobRoute;
            set
            {
                _JobRoute = value;
                NotifyPropertyChanged();
            }
        }
        private string _JobRoute;


        /// <summary>
        /// 任务请求模式
        /// </summary>
        [SugarColumn(ColumnDataType = "varchar(5)", IsNullable = true)]
        public string JobMode
        {
            get =>_JobMode;
            
            set
            {
                _JobMode = value;
                NotifyPropertyChanged();
            }
        }
        private string _JobMode;

        /// <summary>
        ///  格式化后时间列表
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public List<JobTime> Times
        {
            get
            {
                if (JobRunTime != null)
                {
                    List<JobTime> a = new List<JobTime>();
                    foreach (var timeCount in JobRunTime.Split(',').ToList())
                    {
                        a.Add(new JobTime
                        {
                            IsStart = Convert.ToInt32(timeCount.Split(':')[0]),
                            Index = a.Count,
                            JobRunTime = timeCount.Split(':')[1],
                        });
                    }
                    _Times = a;
                }
                return _Times;
            }
            set
            {
                _Times = value;
            }
        }
        private List<JobTime> _Times ;

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); //实现通知刷新
        }
    }
 }
